package org.jet.emall.rbac.transfer;

import org.jet.emall.rbac.dto.AdminPrivilegeDTO;
import org.jet.emall.rbac.vo.AdminPrivilegeVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.LinkedList;
import java.util.List;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-04-10 13:36
 */

@Mapper
public interface AdminPrivilegeVoTransfer {

    AdminPrivilegeVoTransfer INSTANCE = Mappers.getMapper(AdminPrivilegeVoTransfer.class);


    /**
     * 权限DTO转VO
     * @param dto
     * @return
     */
    @Mapping(target = "children",ignore = true)
    AdminPrivilegeVO toVo(AdminPrivilegeDTO dto);

    /**
     * 权限DTO列表转VO列表
     * @param dtoList
     * @return
     */
    default List<AdminPrivilegeVO> toVoList(List<AdminPrivilegeDTO> dtoList){
        List<AdminPrivilegeVO> list = new LinkedList<>();
        dtoList.forEach(dto->{
            AdminPrivilegeVO vo = toVo(dto);
            if(dto.getChildren()!=null && !dto.getChildren().isEmpty()){
                List<AdminPrivilegeVO> children = toVoList(dto.getChildren());
                vo.setChildren(children);
            }
            list.add(vo);
        });
        return list;
    }
}
